//
//  ChangePasswordViewController.m
//  享动
//
//  Created by darker on 16/4/16.
//  Copyright © 2016年 HHUI. All rights reserved.
//

#import "ChangePasswordViewController.h"
#import "RequestManager.h"
#import "LoginViewController.h"

@interface ChangePasswordViewController ()

@property (nonatomic, strong) UITextField *myOldPwd;

@property (nonatomic, strong) UITextField *mySurePwd;

@property (nonatomic, strong) UITextField *myNewPwd;

@property (nonatomic, strong) UILabel *myOld;

@property (nonatomic, strong) UILabel *mySure;

@property (nonatomic, strong) UILabel *myNew;

@property (nonatomic, strong) UIButton *sureButton;

@end

@implementation ChangePasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 点击修改密码 跳转到的页面
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    _myOld = [[UILabel alloc] initWithFrame:CGRectMake(10, 64 + 50, 100, 30)];
    _myOld.font = [UIFont systemFontOfSize:14];
    _myOld.textColor = [UIColor grayColor];
    _myOld.textAlignment = NSTextAlignmentRight;
    _myOld.text = @"旧密码:";
    [self.view addSubview:_myOld];
    
    _myNew = [[UILabel alloc] initWithFrame:CGRectMake(10, 64 + 50 + 40, 100, 30)];
    _myNew.font = [UIFont systemFontOfSize:14];
    _myNew.textColor = [UIColor grayColor];
    _myNew.text = @"新密码:";
    _myNew.textAlignment = NSTextAlignmentRight;
    [self.view addSubview:_myNew];
    
    _mySure = [[UILabel alloc] initWithFrame:CGRectMake(10, 64 + 50 + 80, 100, 30)];
    _mySure.font = [UIFont systemFontOfSize:14];
    _mySure.textColor = [UIColor grayColor];
    _mySure.textAlignment = NSTextAlignmentRight;
    _mySure.text = @"确认密码:";
    [self.view addSubview:_mySure];
    
    _myOldPwd = [[UITextField alloc] initWithFrame:CGRectMake(10 + 140, 64 + 50 , 150, 30)];
    _myOldPwd.placeholder = @"请输入旧密码";
    [_myOldPwd setSecureTextEntry:YES];
    _myOldPwd.font = [UIFont systemFontOfSize:14];
    _myOldPwd.textColor = [UIColor grayColor];
    _myOldPwd.borderStyle = UITextBorderStyleBezel;
    [self.view addSubview:_myOldPwd];
    
    _myNewPwd = [[UITextField alloc] initWithFrame:CGRectMake(10 + 140, 64 + 50 + 40, 150, 30)];
    _myNewPwd.placeholder = @"请设置新密码";
    [_myNewPwd setSecureTextEntry:YES];
    _myNewPwd.font = [UIFont systemFontOfSize:14];
    _myNewPwd.textColor = [UIColor grayColor];
    _myNewPwd.borderStyle = UITextBorderStyleBezel;
    [self.view addSubview:_myNewPwd];
    
    _mySurePwd = [[UITextField alloc] initWithFrame:CGRectMake(10 + 140, 64 + 50 + 80, 150, 30)];
    _mySurePwd.placeholder = @"请再次输入密码";
    [_mySurePwd setSecureTextEntry:YES];
    _mySurePwd.font = [UIFont systemFontOfSize:14];
    _mySurePwd.textColor = [UIColor grayColor];
    _mySurePwd.borderStyle = UITextBorderStyleBezel;
    [self.view addSubview:_mySurePwd];
    
    _sureButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _sureButton.frame = CGRectMake(KWIDTH / 2 - 50, 64 + 50 + 80 + 40, 100, 30);
    [_sureButton setTitle:@"确认" forState:UIControlStateNormal];
    [_sureButton addTarget:self action:@selector(sureButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_sureButton];
    
}

- (void) sureButtonAction
{
    if ([_myOldPwd.text isEqualToString:[[NSUserDefaults standardUserDefaults] objectForKey:@"password"]])
    {
        if ([_myNewPwd.text isEqualToString:_mySurePwd.text])
        {

            NSDictionary *parDic = @{@"_ch":@"appStore", @"_lan":@"ch", @"_nw":@"WiFi", @"_product":@"fitmix", @"_sdk":@"iPhone%20OS8.2",@"_v":@"3", @"nowPwd":_mySurePwd.text, @"oldPwd": [[NSUserDefaults standardUserDefaults] objectForKey:@"password"], @"uid":[[NSUserDefaults standardUserDefaults] objectForKey:@"id"]};
            [RequestManager requestWithUrl:@"http://appt.igeekery.com/user/modify-password.json" requestType:RequestTypePost parDic:parDic finish:^(NSData *data) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"修改密码成功" preferredStyle:UIAlertControllerStyleAlert];
                    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action)
                    {
                        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"isLogin"];
                        LoginViewController *loginVc = [[LoginViewController alloc] initWithNibName:@"login" bundle:[NSBundle mainBundle]];
                        [self presentViewController:loginVc animated:YES completion:nil];
                                                        
                    }];
                    
                    [alertController addAction:sureAction];
                    [self presentViewController:alertController animated:YES completion:nil];
                    
                });

            } error:^(NSError *error) {
                NSLog(@"修改密码时 网络请求错误");
            }];
        }
        else
        {
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"两次输入的密码不一致" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:nil];
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
            [alertController addAction:sureAction];
            [alertController addAction:cancelAction];
            [self presentViewController:alertController animated:YES completion:nil];
        }        
    }
    else
    {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"输入的旧密码有误" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:nil];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        [alertController addAction:sureAction];
        [alertController addAction:cancelAction];
        [self presentViewController:alertController animated:YES completion:nil];
    }
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
